layui.define(['table', 'form','selectN','element'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,selectN = layui.selectN, element = layui.element;

    var placeId = $('input[name="id"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/place/device/list.data'
    ,where:{placeId:placeId}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'deviceSn', title: '设备序列号'}
      ,{field: 'deviceTypeName', title: '设备类型'}
      ,{field: 'deviceFunNames', title: '设备功能'}
      ,{field: 'userInfo', title: '设备所属'}
      ,{field: 'bindNote',title: '绑定信息'}
      ,{field: 'deployDate',title: '部署时间'}
      ,{title: '操作', width: 260, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: false
    ,height: 'full-85'
    ,text: '对不起，加载出现异常！'
  });

    //监听搜索
    form.on('submit(LAY-search)', function(data){
        var field = data.field;
        //执行重载
        table.reload('LAY-table', {
            where: field
        });
    });
  
  //事件
  var active = {
      addDevice: function(){
          parent.layer.open({
              type: 2
              ,title: '新增设备'
              ,content: layui.setter.webRoot + '/place/device/add.html?placeId='+placeId
              ,maxmin: true
              ,area: ['500px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow =  parent.window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field; //获取提交的字段
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/place/device/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  parent.layer.close(index);
                              }else{
                                  parent.layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,editBindNote: function(obj){
          parent.layer.open({
              type: 2
              ,title: '修改设备绑定信息'
              ,content: layui.setter.webRoot + '/place/device/bind_note/update.html?deviceSn='+obj.data.deviceSn
              ,maxmin: true
              ,area: ['500px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = parent.window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field; //获取提交的字段
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/place/device/bind_note/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  parent.layer.close(index);
                              }else{
                                  parent.layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,qrcode: function (obj) {
          parent.layer.open({
              type: 2
              , title: '设备二维码'
              , content: layui.setter.webRoot + '/device/qrcode.html?deviceSn=' + obj.data.deviceSn
              , maxmin: false
              , area: ['400px', '450px']
              , btn: ['取消']
          });
      }
    ,remove:function(obj){
          parent.layer.confirm('确定要移除该设备部署吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/place/device/undeploy.do',
	          data:{deviceSn:obj.data.deviceSn},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
                    parent.layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
            parent.layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('place_device_content', {})
});